package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.core.Camera;
import androidx.camera.core.CameraFilter;
import androidx.camera.core.CameraInfo;
import androidx.camera.core.CameraSelector;
import androidx.camera.core.ImageAnalysis;
import androidx.camera.core.ImageCapture;
import androidx.camera.core.ImageCaptureException;
import androidx.camera.core.Preview;
import androidx.camera.core.concurrent.ConcurrentCamera;
import androidx.camera.lifecycle.ProcessCameraProvider;
import androidx.camera.view.PreviewView;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.camera2.CameraManager;
import android.os.Bundle;
import android.util.LruCache;
import android.view.SurfaceView;
import android.widget.Toast;

import com.example.myapplication.constants.Constant;
import com.example.myapplication.databinding.ActivityMainBinding;
import com.example.myapplication.utils.CommonUtil;
import com.google.common.util.concurrent.ListenableFuture;

import java.io.File;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;


public class MainActivity extends AppCompatActivity {


    private ActivityMainBinding binding;

    private static final String[] REQUIRED_PERMISSIONS = {
            Manifest.permission.CAMERA,
            Manifest.permission.RECORD_AUDIO
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        requestPermissions();
        initCamera();
        initEvent();
    }

    private void initEvent() {
        binding.btnClick.setOnClickListener(v -> {
            CommonUtil.jumpToAnotherActivity(this, PictureActivity.class);
        });
    }

    private void testCa() {
    }

    private void requestPermissions() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, Constant.REQUEST_CAMERA_PERMISSION);
        } else {
            Toast.makeText(this, "所有权限均已申请", Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == Constant.REQUEST_CAMERA_PERMISSION) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 相机权限已授予
                Toast.makeText(this, "所有权限均已申请", Toast.LENGTH_SHORT).show();
            } else {
                // 相机权限被拒绝
                Toast.makeText(this, "请同意权限申请", Toast.LENGTH_SHORT).show();
                requestPermissions();
            }
        }
    }

    private void initCamera() {
        ListenableFuture<ProcessCameraProvider> future = ProcessCameraProvider.getInstance(this);
        future.addListener(() -> {
            try {
                ProcessCameraProvider processCameraProvider = future.get();
                Preview preview = new Preview.Builder().build();
                preview.setSurfaceProvider(binding.preview.getSurfaceProvider());
                CameraSelector defaultBackCamera = CameraSelector.DEFAULT_BACK_CAMERA;
                processCameraProvider.unbind();
                processCameraProvider.bindToLifecycle(MainActivity.this, defaultBackCamera, preview);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }, ContextCompat.getMainExecutor(this));

    }


}